<template>
  <div class="login">
    <div class="login-container">
      <div class="left-section">
        <div class="animation-container">
          <lottie-player
              src="https://lottie.host/b00aba23-4316-427f-814e-518d0f27e815/IJ9gf6Yy3M.json"
              background="transparent"
              speed="1"
              style="width: 100%; height: 450px;"
              loop
              autoplay
          ></lottie-player>
        </div>
        <h2 class="slogan">快捷 · 舒适 · 安全</h2>
        <p class="sub-slogan">开启您的智慧出行之旅</p>
      </div>
      <div class="right-section">
        <div class="login-card">
          <h1 class="login-title">行易达</h1>
          <p class="login-subtitle">铁路出行，轻松便捷</p>
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin">
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled">
              <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter.native="handleLogin">
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img"/>
              </div>
            </el-form-item>
            <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
            <el-form-item style="width:100%;">
              <el-button :loading="loading" size="medium" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              <div style="float: right;" v-if="register">
                <router-link class="link-type" :to="'/register'">立即注册</router-link>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <div class="el-login-footer">
      <span>Copyright © 2024 行易达 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
    // 添加 Lottie 动画脚本
    const script = document.createElement('script');
    script.src = "https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js";
    document.head.appendChild(script);
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #1890ff, #69c0ff);
}

.login-container {
  display: flex;
  width: 1000px;
  height: 600px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
  overflow: hidden;
}

.left-section {
  flex: 1.4;
  background-color: #5CB6FF;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.animation-container {
  width: 100%;
  max-width: 600px;
}

.slogan {
  font-size: 36px;
  font-weight: bold;
  color: #fff;
  margin-top: 20px;
  text-align: center;
}

.sub-slogan {
  font-size: 18px;
  color: #fff;
  margin-top: 10px;
  text-align: center;
}

.right-section {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
}

.login-card {
  width: 100%;
  max-width: 400px;
}

.login-title {
  font-size: 36px;
  font-weight: bold;
  color: #1890ff;
  text-align: center;
  margin-bottom: 12px;
}

.login-subtitle {
  font-size: 18px;
  color: #8c8c8c;
  text-align: center;
  margin-bottom: 40px;
}

.login-form {
  :deep(.el-input) {
    height: 42px;

    input {
      height: 42px;
      padding-left: 15px;
      border-radius: 4px;
      background-color: #f5f7fa;
      border: 1px solid #e4e7ed;

      &:focus {
        border-color: #1890ff;
      }
    }
  }

  :deep(.input-icon) {
    height: 42px;
    width: 14px;
    margin-left: 2px;
    color: #909399;
  }

  :deep(.el-button--primary) {
    background: #1890ff;
    border-color: #1890ff;
    height: 42px;
    font-size: 16px;

    &:hover {
      background: #40a9ff;
      border-color: #40a9ff;
    }
  }
}

.login-code {
  width: 33%;
  height: 42px;
  float: right;

  img {
    height: 42px;
    cursor: pointer;
    border-radius: 4px;
  }
}

:deep(.el-checkbox) {
  color: #606266;
}

.el-login-footer {
  margin-top: 20px;
  color: #fff;
  font-size: 12px;
}

.link-type {
  color: #1890ff;
  text-decoration: none;

  &:hover {
    color: #40a9ff;
  }
}
</style>

